<?php
/**
 * Created by cmb_weixin.master.
 * File: goodsOrderTrace.php
 * User: xuewl
 * Date: 2017/12/13
 * Time: 01:56
 */

namespace app\goods\service;
use app\common\library\Service;

class goodsOrderTrace extends Service
{

    use \app\common\library\traits\Model;
    protected $model;

    public function _initialize() {
        parent::_initialize();
        $this->model = model('goods/goods_order_trace');
    }


    /**
     * 创建日志
     * @param string $type
     * @param $order_no
     * @param array $attr
     * @return mixed
     */
    public function create($type = '', $order, $attr = []) {

        $config = config('goods.goods_order_field_content');
        $content = $config[$type];

        $replace = [];
        $data = $order->toArray();

        foreach ($data as $key => $val) {
            $replace['{{'. $key .'}}'] = $val;
        }

        $info = [
            'uid' => 0,
            'name' => '',
            'type' => $type,
            'order_no' => $order->order_no,
            'content' => str_replace(array_keys($replace), $replace, $content),
            'is_system' => 0,
        ];

        $trace = $this->save($info, []);
        if(!$trace) {
            $this->error = $this->getError();
            return false;
        }
        return $trace;
    }
}